﻿/*
 *  Copyright (C) 2010 by Benjamin Nolmans <benjamin.nolmans@gmail.com>
 *  and Barbara Post <postb99@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using System;
using System.Web.Services;
using System.Web.UI;
using OsamesFrameworkCore;
using OsamesFrameworkCore.Logging;
using OsamesFrameworkCore.Plugins;
using OsamesFrameworkCore.Plugins.Management.Types;
using OsamesFrameworkCore.Tools;

namespace OsamesFrameworkWeb
{
    /// <summary>
    /// Service web principal de l'application web Osames.
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // Pour autoriser l'appel de ce service Web depuis un script à l'aide d'ASP.NET AJAX, supprimez les marques de commentaire de la ligne suivante.
    [System.Web.Script.Services.ScriptService]
    public class OsamesWebService : WebService, ILoggable
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        public OsamesWebService()
        {
            Log(LogLevel.Trace, "OsamesWebService : constructor");
        }

        /// <summary>
        /// Logging facility.
        /// </summary>
        /// <param name="strMessage_">Message</param>
        /// <param name="strMessageDetails_">Optional array of message details. In this case, strMessage_ parameter contains placeholders, each matching a value in strMessageDetails_</param>
        /// <param name="eLogLevel_">Log level</param>
        public void Log(LogLevel eLogLevel_, string strMessage_, object[] strMessageDetails_ = null)
        {
            LoggerService.Log(this, eLogLevel_, strMessage_, strMessageDetails_);
        }

        /// <summary>
        /// Logging facility.
        /// </summary>
        /// <param name="eLogLevel_">Osames log level</param>
        /// <param name="strMessage_">Message</param>
        /// <param name="ex_">Exception we will log message and stack trace of</param>
        public void Log(LogLevel eLogLevel_, string strMessage_, Exception ex_)
        {
            LoggerService.Log(this, eLogLevel_, strMessage_, ex_);
        }

        [WebMethod]
        [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]   //authorize http get method.
        public string HelloWorld()
        {
            try
            {
                Log(LogLevel.Info, "OsamesWebService : HelloWorld web method call...");
                Log(LogLevel.Trace, "OsamesWebService web method : available plugins count : {0}", new object[] { OsamesFrameworkCore.Plugins.Management.PluginsManager.Instance.DicAvailablePluginSet.Count });

                const string strPluginName = "HelloWorld Plugin";

                AvailablePlugin selectedPlugin = OsamesFrameworkCore.Plugins.Management.PluginsManager.Instance.Find(strPluginName);  //<---- Name property

                Log(LogLevel.Trace, "HelloWorld");
                //lance le traitement interne au plugin.
                if (selectedPlugin == null)
                {
                    Log(LogLevel.Error, "OsamesWebService : HelloWorld : plugin with name '{0}' not found", new object[] { strPluginName });
                    return "sniff : plugin not found (lookup by name, see logs)";
                }
                selectedPlugin.Plugin.Execute("Hello", null);
                IWebPlugin webPlugin = selectedPlugin.WebPlugin;
                if (webPlugin != null)
                {
                    Control ctlOutput = webPlugin.GetOutput();
                    if (ctlOutput != null)
                        return WebTools.RenderControl(ctlOutput);
                    return "";
                }
                return "Oops, not a web plugin";
                // TODO [web service / gestion messages] à réflechir : si le plugin n'est pas web mais qu'on veut indiquer que le traitement est ok ou nok...
            }
            catch (Exception ex)
            {
                Log(LogLevel.Trace, "Exception from HelloWorldPlugin", ex);
                return string.Format("Oops, error occured : {0}", ex.Message);
            }
        }
    }
}